package com.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.demo.common.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.entity.Menu;
import com.demo.service.IMenuService;
import com.demo.common.dto.PageDTO;
import com.demo.dto.MenuDTO;
import com.demo.query.MenuQueryDTO;
import com.demo.vo.MenuVO;

import java.util.List;

@Tag(name = "菜单权限表管理")
@RestController
@RequestMapping("/menu")
public class MenuController {

    private final IMenuService menuService;

    public MenuController(IMenuService menuService) {
        this.menuService = menuService;
    }

    @SaCheckPermission("btn.queryMenu")
    @Operation(summary = "查询所有菜单权限表信息")
    @GetMapping("list")
    public SaResult list() {
        List<Menu> menuList = menuService.list();
        List<MenuVO> result = BeanUtil.copyToList(menuList, MenuVO.class);
        return SaResult.ok().setData(result);
    }

    @SaCheckPermission("btn.queryMenu")
    @Operation(summary = "分页查询菜单权限表信息")
    @PostMapping("page")
    public SaResult page(@RequestBody MenuQueryDTO menuQueryDTO) {
        Integer currentPage = menuQueryDTO.getCurrentPage();
        Integer pageSize = menuQueryDTO.getPageSize();

        LambdaQueryWrapper<Menu> queryWrapper = menuQueryDTO.buildQueryWrapper();
        Page<Menu> menuPage = menuService.page(new Page<>(currentPage, pageSize), queryWrapper);

        PageVO<MenuVO> menuPageVO = PageDTO.getPageVO(menuPage, item -> BeanUtil.copyProperties(item, MenuVO.class));
        return SaResult.ok().setData(menuPageVO);
    }

    @SaCheckPermission(value = {"btn.addMenu", "btn.editMenu"}, mode = SaMode.OR)
    @Operation(summary = "新增/修改菜单权限表信息")
    @PostMapping("createOrEdit")
    public SaResult createOrEdit(@RequestBody MenuDTO menuDTO) {
        Menu menu = BeanUtil.copyProperties(menuDTO, Menu.class);
        boolean result = menuService.saveOrUpdate(menu);
        return SaResult.ok().setData(result);
    }

    @SaCheckPermission("btn.queryMenu")
    @Operation(summary = "根据id获取菜单权限表信息")
    @GetMapping("getById/{id}")
    public SaResult getById(@Parameter(description = "菜单权限表id")
                            @PathVariable Integer id) {
        Menu menu = menuService.getById(id);
        if (menu == null) {
            return SaResult.error("数据不存在");
        }
        MenuVO result = BeanUtil.copyProperties(menu, MenuVO.class);
        return SaResult.ok().setData(result);
    }

    @SaCheckPermission("btn.deleteMenu")
    @Operation(summary = "根据id删除菜单权限表信息")
    @DeleteMapping("remove/{id}")
    public SaResult remove(@Parameter(description = "菜单权限表id")
                           @PathVariable Integer id) {
        boolean result = menuService.removeById(id);
        return SaResult.ok(result ? "删除成功" : "删除失败").setData(result);
    }

    @SaCheckPermission("btn.deleteMenu")
    @Operation(summary = "根据id批量删除菜单权限表信息")
    @DeleteMapping("/removeBatch/{ids}")
    public SaResult removeBatch(@Parameter(description = "id集合")
                                @PathVariable List<Integer> ids) {
        boolean result = menuService.removeByIds(ids);
        return SaResult.ok(result ? "删除成功" : "删除失败").setData(result);
    }
}
